package com.example.base.net

import com.blankj.utilcode.util.SPUtils
import okhttp3.OkHttpClient
import okhttp3.logging.HttpLoggingInterceptor
import retrofit2.Retrofit
import retrofit2.converter.gson.GsonConverterFactory
import java.util.concurrent.TimeUnit

/**
 * 网络请求工具类
 */
object RetrofitUtils {
    fun getRetrofit():Retrofit{
        val okHttpClient = OkHttpClient.Builder()
            .readTimeout(5,TimeUnit.SECONDS)
            .writeTimeout(5,TimeUnit.SECONDS)
            .connectTimeout(5,TimeUnit.SECONDS)
            .addInterceptor(HttpLoggingInterceptor().setLevel(HttpLoggingInterceptor.Level.BODY))
            /**
             * 自定义请求头拦截器（但拦截器复杂时，可以创建一个文件来实现）
             */
            /* .addInterceptor {
                val token = SPUtils.getInstance().getString("token")
                val request = it.request().newBuilder().addHeader("Authori-zation",token).build()
                it.proceed(request)
            }*/
            .addInterceptor(HeaderInterceptor())
            //.addInterceptor(HeaderVideoInterceptor())
            .build()
        val retrofit = Retrofit.Builder()
            .baseUrl(Const.BASE_URL)
            .client(okHttpClient)
            .addConverterFactory(GsonConverterFactory.create())
            .build()
        return  retrofit
    }
}